<template>
  <div :id="id" :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts'

export default {
  props: {
    className: {
      type: String,
      default: 'piont-chart'
    },
    id: {
      type: String,
      default: 'piont-chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    data: {
      type: Object,
      require: true
    },
    title: {
      type: String,
      default: '各站点超重数分析'
    },
    queryTime: {
      type: String,
      default: '近三天'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', () => {
      this.chart.resize()
    })
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      const that = this
      this.chart = echarts.init(document.getElementById(this.id))
      this.chart.setOption({
        backgroundColor: '#F3F9FF',
        title: [
          {
            text: that.title,
            textStyle: {
              fontSize: 18
            },
            left: 10,
            top: 10
          }
        ],
        graphic: [
          {
            type: 'text',
            left: 250,
            top: 10,
            style: {
              text: this.queryTime,
              textAlign: 'center',
              fontSize: 14
            }
          },
          {
            type: 'text',
            left: 450,
            top: 10,
            style: {
              text: `车次总数: ${this.data.totalVehicleCount}`,
              textAlign: 'center',
              fontSize: 14
            }
          },
          {
            type: 'text',
            left: 580,
            top: 10,
            style: {
              text: `超重车次总数: ${this.data.totalOverloadCount}`,
              textAlign: 'center',
              fontSize: 14
            }
          },
          {
            type: 'text',
            left: 740,
            top: 10,
            style: {
              text: `总超重比: ${this.data.totalOverloadCount ? (this.data.totalOverloadCount / this.data.totalVehicleCount * 100).toFixed(2) : 0}%`,
              textAlign: 'center',
              fontSize: 14
            }
          }
        ],
        legend: {
          left: 'right',
          top: 0
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          },
          formatter: function(params) {
            console.log(params)
            return params[0].axisValue + '<br/>' + params[0].seriesName + ' : ' + params[0].value[1] + '<br/>' + params[1].seriesName + ' : ' + params[1].value[1] + '<br/>' + '超重比 : ' + (params[1].value[1] / params[0].value[1] * 100).toFixed(2) + '%'
          }
        },
        color: ['#006EFF', '#F9AF00'],
        xAxis: {
          type: 'category'
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '未超重数',
            type: 'bar',
            stack: 'Ad',
            emphasis: {
              focus: 'series'
            },
            barMaxWidth: 34,
            data: that.data.normalCount
          },
          {
            name: '超重数',
            type: 'bar',
            stack: 'Ad',
            emphasis: {
              focus: 'series'
            },
            barMaxWidth: 34,
            data: that.data.overloadCount
          }
        ],
        toolbox: {
          // x: 'right',
          // y: 'bottom',
          right: 20,
          bottom: 0,
          feature: {
            saveAsImage: {}
          }
        }
      })

      window.addEventListener('resize', () => {
        if (this.chart) {
          this.chart.resize()
        }
      })
    }
  }
}
</script>
